/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * topicsPanel.java
 *
 * Created on 22/05/2011, 19:18:23
 */

package ClientGui;

import clientData.TopicData;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author USER
 */
public class TopicsPanel extends javax.swing.JPanel {

    /** Creates new form topicsPanel */
    public TopicsPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AddText = new javax.swing.JTextField();
        AddButton = new javax.swing.JButton();
        addLabel2 = new javax.swing.JLabel();
        addLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        addText2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        topicList = new javax.swing.JList();
        AddLable = new javax.swing.JLabel();
        AddTopicButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        contentPane = new javax.swing.JTextPane();

        AddText.setEnabled(false);

        AddButton.setText("Add");
        AddButton.setEnabled(false);
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        addLabel2.setText("Post Title");
        addLabel2.setEnabled(false);

        addLabel3.setText("Post Content");
        addLabel3.setEnabled(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Choose Topic :");

        addText2.setEnabled(false);

        topicList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                topicListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(topicList);

        AddLable.setText("Topic Title");
        AddLable.setEnabled(false);

        AddTopicButton.setText("Add New Topic");
        AddTopicButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddTopicButtonActionPerformed(evt);
            }
        });

        contentPane.setEnabled(false);
        jScrollPane3.setViewportView(contentPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AddTopicButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addLabel2)
                            .addComponent(AddLable)
                            .addComponent(addLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(AddButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(AddText)
                            .addComponent(addText2)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                        .addComponent(AddTopicButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(AddLable)
                            .addComponent(AddText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addLabel2)
                            .addComponent(addText2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(addLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(AddButton))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(53, 53, 53))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        if (this.AddText.getText().equals("")  || this.addText2.getText().equals("")  || this.contentPane.getText().equals("") ){
                JOptionPane.showMessageDialog(new JFrame(), "You must fill in all Fields!" , "Dialog", JOptionPane.ERROR_MESSAGE);
        } else {
            Controller.addTopic(this.AddText.getText(),this.addText2.getText(),this.contentPane.getText());
            this.clearFields();
        }
}//GEN-LAST:event_AddButtonActionPerformed

    private void topicListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_topicListMouseClicked
        TopicData td = (TopicData)this.getList().getSelectedValue();
        if (td!=null){
            this.setVisible(false);
            Controller.showTopic(td);
        }
}//GEN-LAST:event_topicListMouseClicked

    private void AddTopicButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddTopicButtonActionPerformed
        this.enableFields(true);
}//GEN-LAST:event_AddTopicButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JLabel AddLable;
    private javax.swing.JTextField AddText;
    private javax.swing.JButton AddTopicButton;
    private javax.swing.JLabel addLabel2;
    private javax.swing.JLabel addLabel3;
    private javax.swing.JTextField addText2;
    private javax.swing.JTextPane contentPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList topicList;
    // End of variables declaration//GEN-END:variables


    public javax.swing.JList getList() {
        return this.topicList;
    }

    public  void enableFields(boolean bool) {
        this.AddText.setEnabled(bool);
        this.addText2.setEnabled(bool);
        this.contentPane.setEnabled(bool);
        this.addLabel2.setEnabled(bool);
        this.addLabel3.setEnabled(bool);
        this.AddLable.setEnabled(bool);
        this.AddButton.setEnabled(bool);
    }

    public void clearFields(){
        this.AddText.setText("");
        this.addText2.setText("");
        this.contentPane.setText("");
        enableFields(false);
    }
}
